package com.recycle.system.service.impl;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.recycle.common.utils.DateUtils;
import com.recycle.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.recycle.system.mapper.PolicyMapper;
import com.recycle.system.domain.Policy;
import com.recycle.system.service.IPolicyService;

/**
 * 政策信息Service业务层处理
 *
 * @author recycle
 * @date 2023-03-11
 */
@Service
public class PolicyServiceImpl implements IPolicyService
{
    @Autowired
    private PolicyMapper policyMapper;

    /**
     * 查询政策信息
     *
     * @param policyId 政策信息主键
     * @return 政策信息
     */
    @Override
    public Policy selectPolicyByPolicyId(Long policyId)
    {
        return policyMapper.selectPolicyByPolicyId(policyId);
    }

    /**
     * 查询政策信息列表
     *
     * @param policy 政策信息
     * @return 政策信息
     */
    @Override
    public List<Policy> selectPolicyList(Policy policy)
    {
        return policyMapper.selectPolicyList(policy);
    }

    /**
     * 模糊查询政策信息列表
     *
     * @param policy 政策信息
     * @return 政策信息集合
     */
    @Override
    public List<Policy> fuzzySelectPolicyList(Policy policy) {
        QueryWrapper wrapper = new QueryWrapper<Policy>();
        Stream<Field> stream = Arrays.stream(Policy.class.getDeclaredFields());
        stream.forEach(field -> {
            field.setAccessible(true);
            if(field.getType() != String.class) {
                return;
            }
            try {
                String value = (String) field.get(policy);
                if (StringUtils.isNotEmpty(value)) {
                    if(field.getName().equals("typeName")){
                        wrapper.like("type_name",value);
                        return;
                    }
                    wrapper.like(field.getName(), value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        if (policy.getBeginDate() != null && policy.getEndDate() != null) {

            wrapper.between("publish_time", policy.getBeginDate(), policy.getEndDate());

        }
        return policyMapper.selectList(wrapper);
    }

    /**
     * 新增政策信息
     *
     * @param policy 政策信息
     * @return 结果
     */
    @Override
    public int insertPolicy(Policy policy)
    {
        policy.setCreateTime(DateUtils.getNowDate());
        return policyMapper.insertPolicy(policy);
    }

    /**
     * 修改政策信息
     *
     * @param policy 政策信息
     * @return 结果
     */
    @Override
    public int updatePolicy(Policy policy)
    {
        policy.setUpdateTime(DateUtils.getNowDate());
        return policyMapper.updatePolicy(policy);
    }

    /**
     * 批量删除政策信息
     *
     * @param policyIds 需要删除的政策信息主键
     * @return 结果
     */
    @Override
    public int deletePolicyByPolicyIds(Long[] policyIds)
    {
        return policyMapper.deletePolicyByPolicyIds(policyIds);
    }

    /**
     * 删除政策信息信息
     *
     * @param policyId 政策信息主键
     * @return 结果
     */
    @Override
    public int deletePolicyByPolicyId(Long policyId)
    {
        return policyMapper.deletePolicyByPolicyId(policyId);
    }
}
